using System;
using System.Linq;
using System.Runtime.CompilerServices;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
	public class SubsystemLJViews : SubsystemViews
	{
		private SubsystemRemotePlayers subsystemRemotePlayers_;

		protected override void Load(ValuesDictionary valuesDictionary)
		{
			SubsystemPlayers subsystemPlayers = ((Subsystem)this).Project.FindSubsystem<SubsystemPlayers>(true);
			ReflectionUtils.SetField((object)this, "m_subsystemPlayers", (object)subsystemPlayers);
			ReflectionUtils.SetProperty((object)this, "SubsystemTerrain", (object)((Subsystem)this).Project.FindSubsystem<SubsystemTerrain>(true));
			ReflectionUtils.SetProperty((object)this, "GamesWidget", (object)valuesDictionary.GetValue<GamesWidget>("GamesWidget"));
			subsystemRemotePlayers_ = ((Subsystem)this).Project.FindSubsystem<SubsystemRemotePlayers>(true);
			subsystemPlayers.PlayerAdded += new Action<PlayerData>(_003CLoad_003Eb__1_0);
			subsystemPlayers.PlayerRemoved += new Action<PlayerData>(_003CLoad_003Eb__1_1);
			PlayerData playerData = Enumerable.FirstOrDefault(subsystemPlayers.PlayersData, new Func<PlayerData, bool>(_003CLoad_003Eb__1_2));
			if (playerData != null)
			{
				playerData.InputDevice = WidgetInputDevice.Gamepads | WidgetInputDevice.Keyboard | WidgetInputDevice.Mouse | WidgetInputDevice.Touch;
				ReflectionUtils.InvokeMethod((object)this, "AddViewForPlayer", new object[1] { playerData });
				{
					foreach (PlayerData playersDatum in subsystemPlayers.PlayersData)
					{
						if (playersDatum != playerData)
						{
							playersDatum.InputDevice = WidgetInputDevice.None;
							ReflectionUtils.InvokeMethod((object)this, "AddViewForPlayer", new object[1] { playersDatum });
						}
					}
					return;
				}
			}
			foreach (PlayerData playersDatum2 in subsystemPlayers.PlayersData)
			{
				ReflectionUtils.InvokeMethod((object)this, "AddViewForPlayer", new object[1] { playersDatum2 });
			}
		}

		private void AddMainPlayer(PlayerData mainPlayer)
		{
			foreach (View view in ((SubsystemViews)this).Views)
			{
				ReflectionUtils.InvokeMethod((object)this, "RemoveView", new object[1] { view });
			}
			mainPlayer.InputDevice = WidgetInputDevice.Gamepads | WidgetInputDevice.Keyboard | WidgetInputDevice.Mouse | WidgetInputDevice.Touch;
			ReflectionUtils.InvokeMethod((object)this, "AddViewForPlayer", new object[1] { mainPlayer });
			foreach (PlayerData playersDatum in ((Subsystem)this).Project.FindSubsystem<SubsystemPlayers>().PlayersData)
			{
				if (playersDatum != mainPlayer)
				{
					playersDatum.InputDevice = WidgetInputDevice.None;
					ReflectionUtils.InvokeMethod((object)this, "AddViewForPlayer", new object[1] { playersDatum });
				}
			}
		}

		[CompilerGenerated]
		private void _003CLoad_003Eb__1_0(PlayerData playerData)
		{
			if (subsystemRemotePlayers_.IsMainPlayer(playerData))
			{
				AddMainPlayer(playerData);
				return;
			}
			ReflectionUtils.InvokeMethod((object)this, "AddViewForPlayer", new object[1] { playerData });
		}

		[CompilerGenerated]
		private void _003CLoad_003Eb__1_1(PlayerData playerData)
		{
			if (playerData.View != null)
			{
				ReflectionUtils.InvokeMethod((object)this, "RemoveView", new object[1] { playerData.View });
			}
		}

		[CompilerGenerated]
		private bool _003CLoad_003Eb__1_2(PlayerData d)
		{
			return subsystemRemotePlayers_.IsMainPlayer(d);
		}
	}
}
